package com.yuan.model_convenient.service.impl;

import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_convenient.domain.ConvenientComplaint;
import com.yuan.model_convenient.repository.ConvenientComplaintDao;
import com.yuan.model_convenient.service.ConvenientComplaintService;
import com.yuan.model_convenient.service.dto.ConvenientComplaintDto;
import com.yuan.model_convenient.service.mapstruct.ConvenientComplaintMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Service
@RequiredArgsConstructor
public class ConvenientComplainServiceImpl implements ConvenientComplaintService {

    private final ConvenientComplaintDao convenientComplaintDao;

    private final ConvenientComplaintMapper convenientComplaintMapper;

    @Override
    public PageResult<ConvenientComplaintDto> queryAll(CommonQueryCriteria criteria, Pageable pageable) {
        Page<ConvenientComplaint> page = convenientComplaintDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        for (ConvenientComplaint user : page) {
            String niceNameByUserId = convenientComplaintDao.findNiceNameByUserId(user.getLiverId());
            user.setLiverName(niceNameByUserId);
        }
        return PageUtil.toPage(page.map(convenientComplaintMapper::toDto));
    }

    @Override
    public List<ConvenientComplaintDto> queryAll(CommonQueryCriteria criteria) {
        List<ConvenientComplaint> users = convenientComplaintDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        for (ConvenientComplaint user : users) {
            String niceNameByUserId = convenientComplaintDao.findNiceNameByUserId(user.getLiverId());
            user.setLiverName(niceNameByUserId);
        }
        return convenientComplaintMapper.toDto(users);
    }

    @Override
    public void download(List<ConvenientComplaintDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ConvenientComplaintDto complaintDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ConvenientComplaint resources) {
        convenientComplaintDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ConvenientComplaint resources) throws Exception {
        ConvenientComplaint convenientComplaint = convenientComplaintDao.findById(resources.getComplaintId()).orElseGet(ConvenientComplaint::new);
        BeanUtils.copyProperties(resources, convenientComplaint);
        convenientComplaintDao.save(convenientComplaint);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        convenientComplaintDao.deleteById(ids);
    }
}
